package com.collabscm.pf.util;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.collabscm.pf.exception.DependenceLibException;

/**
 * 邮件工具类
 * @author Zeng Raoxing
 */
public class MailUtils {
	/**
	 * mail发送器
	 */
	private static JavaMailSender mailSender;
	private static String systemMail;

	/**
	 * 发送邮件
	 * @param sendTo 发送目录
	 * @param subject 邮件标题
	 * @param content 邮件正文
	 */
	public static void sendMail(String sendTo, String subject, String content) {
		MimeMessage mimeMsg = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(mimeMsg, true, "UTF-8");
			helper.setTo(sendTo);
			helper.setFrom(systemMail);
			helper.setSubject(subject);
			helper.setText(content);
		} catch (MessagingException e) {
			throw new DependenceLibException("mail.jar", "构建发送内容时发生异常", e);
		}
		mailSender.send(mimeMsg);
	}

	/**
	 * 邮件工具的配置类
	 * @author Zeng Raoxing
	 */
	public static class Configuration {

		/**
		 * 设置邮件发送器
		 * @param mailSender
		 */
		protected void setMailSender(JavaMailSender mailSender) {
			MailUtils.mailSender = mailSender;
		}
		
		protected void setSystemMail(String systemMail) {
			MailUtils.systemMail = systemMail;         
		}

	}

}
